package com.myh.cc.coom.main.view

import android.content.Intent
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import com.myh.cc.R
import com.myh.cc.base.BaseActivity
import com.myh.cc.coom.main.adapter.MainFragmentPagerAdapter
import com.myh.cc.coom.main.net.MainControlImpl
import com.myh.cc.coom.main.viewmodel.MainModel
import com.myh.cc.coom.ranking.view.RankingFragment
import com.myh.cc.coom.recommend.view.RecommendFragment
import com.myh.cc.coom.search.view.SearchFragment
import com.myh.cc.coom.singer.view.SingerFragment
import com.myh.cc.databinding.ActivityMainBinding

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/11.   ：
 */
class MainActivity: BaseActivity<ActivityMainBinding,MainControlImpl,MainModel>(){

    override fun getLayoutId(): Int {

        return R.layout.activity_main
    }

    override fun initView() {

        initTabLayout()
        initViewPager()
        initPlayView()
    }

    /**
     * 初始化TabLayout
     */
    private fun initTabLayout(){

        mBinder.tabLayout.addTab(mBinder.tabLayout.newTab().setText(R.string.main_recommend))

        mBinder.tabLayout.addTab(mBinder.tabLayout.newTab().setText(R.string.main_ranking_list))

        mBinder.tabLayout.addTab(mBinder.tabLayout.newTab().setText(R.string.main_singer))

        mBinder.tabLayout.addTab(mBinder.tabLayout.newTab().setText(R.string.main_search))

        
        mBinder.tabLayout.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {
                //mBinder.mainVp.setCurrentItem(tab!!.position)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                //mBinder.mainVp.setCurrentItem(tab!!.position)
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
               mBinder.mainVp.setCurrentItem(tab!!.position)
            }
        })

    }

    /**
     * 初始化ViewPager
     */
    private fun initViewPager(){

        var  listF = ArrayList<Fragment>()
        var  fragment1: Fragment = RecommendFragment()
        var  fragment2: Fragment = RankingFragment()
        var  fragment3: Fragment = SingerFragment()
        var  fragment4: Fragment = SearchFragment()


        listF.add(fragment1)
        listF.add(fragment2)
        listF.add(fragment3)
        listF.add(fragment4)

        mBinder.mainVp.adapter = MainFragmentPagerAdapter(supportFragmentManager,listF);
        mBinder.mainVp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{

            override fun onPageScrollStateChanged(state: Int) {


            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                // 在ViewPager 页面改动的时候 ，设置TabLayout 联动
                mBinder.tabLayout.getTabAt(position)!!.select()
            }
        })

    }


    /**
     *
     */
    private fun initPlayView(){

        mBinder.bottomPlay.root.setOnClickListener { view ->

            val intent = Intent(getContext(),PlayActivity::class.java)
            intent.putExtra("mid","")
            startActivity(intent)
        }

    }
}